/**
 * 软件著作权：
 *
 * 系统名称：  云序列
 *
 * 文件名称：  ImpulseSenderDao.java
 *
 * 功能描述：  发号器数据访问对象
 * 
 * 版本历史：
 * 
 * 2015-10-11   1.0.0版 （龙色波）（创建文件）
 */
package com.dfhc.base.impulsesender.dao;

import java.sql.Date;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.quickbundle.project.RmProjectHelper;
import org.quickbundle.project.common.vo.RmCommonVo;
import org.quickbundle.third.mybatis.RmSqlSessionDaoSupport;
import org.springframework.stereotype.Repository;

import com.dfhc.base.domain.IDomainConstants;
import com.dfhc.base.impulsesender.IImpulseSenderConstants;
import com.dfhc.base.impulsesender.vo.ImpulseSenderVo;
import com.dfhc.util.StringHelper;

/**
 * 发号器数据访问对象
 * 
 * @author 龙色波
 * @see 参见的类
 */
@Repository
public class ImpulseSenderDao extends RmSqlSessionDaoSupport implements IImpulseSenderConstants {
	//插入语句
	private final static String SQL_INSERT = "insert into "+TABLE_NAME+"("+FIELD_DOMAIN_ID+","+FIELD_SEQUENCE_NAME+","+FIELD_UPDATE_DATE+","+FIELD_CYCLE_TYPE+","+FIELD_CURRENT_VALUE+","+FIELD_CURRENT_FMT_VALUE+","+FIELD_SEQUENCE_RULE+","+FIELD_SEQUENCE_LEN+","+FIELD_REMARK+") values(?,?,?,?,?,?,?,?,?)";
	//删除语句
	private final static String SQL_DELETE = "delete from "+TABLE_NAME+" where "+FIELD_DOMAIN_ID +"=? and "+FIELD_SEQUENCE_NAME+"=?";
	//更新语句(更新日期，当前格式化值不能修改)
	//private final static String SQL_UPDATE = "update "+TABLE_NAME+" set "+FIELD_DOMAIN_ID+"=?,"+FIELD_SEQUENCE_NAME+"=?,"+FIELD_UPDATE_DATE+"=?,"+FIELD_CYCLE_TYPE+"=?,"+FIELD_CURRENT_VALUE+"=?,"+FIELD_CURRENT_FMT_VALUE+"=?,"+FIELD_SEQUENCE_RULE+"=?,"+FIELD_SEQUENCE_LEN+"=?,"+FIELD_REMARK+"=? where "+FIELD_DOMAIN_ID +"=? and "+FIELD_SEQUENCE_NAME+"=?";
	private final static String SQL_UPDATE = "update "+TABLE_NAME+" set "+FIELD_DOMAIN_ID+"=?,"+FIELD_SEQUENCE_NAME+"=?,"+FIELD_CYCLE_TYPE+"=?,"+FIELD_CURRENT_VALUE+"=?,"+FIELD_SEQUENCE_RULE+"=?,"+FIELD_SEQUENCE_LEN+"=?,"+FIELD_REMARK+"=? where "+FIELD_DOMAIN_ID +"=? and "+FIELD_SEQUENCE_NAME+"=?";
	//根据pk查询
	private final static String SQL_GETBYID= "select sender.*,domain.name as domain_name from "+TABLE_NAME+" sender,"+IDomainConstants.TABLE_NAME+" domain where  "+FIELD_DOMAIN_ID +"=? and "+FIELD_SEQUENCE_NAME+"=? and sender."+FIELD_DOMAIN_ID+"=domain."+IDomainConstants.FIELD_ID;
    /**
     * 插入单条记录，用id作主键
     * 
     * @param vo 用于添加的VO对象
     * @return 若添加成功，返回新生成的id
     */
    public String insert(ImpulseSenderVo vo) {
    	Object values[]={vo.getDomainId(),vo.getSequenceName(),vo.getUpdateDate(),vo.getCycleType(),vo.getCurrentValue(),vo.getCurrentFmtValue(),vo.getSequenceRule(),vo.getSequenceLen(),vo.getRemark()};
        RmProjectHelper.getCommonServiceInstance().doUpdate(SQL_INSERT, values);
        return null;
    }

    /**
     * 批更新插入多条记录，用id作主键
     * 
     * @param vos 添加的VO对象数组
     * @return 若添加成功，返回新生成的id数组
     */
    public String[] insert(ImpulseSenderVo[] vos) {
    	Object[][] values;
        values= new Object[vos.length][];
        for(int i=0;i<values.length;i++){
        	values[i] = new Object[9];
        	values[i][0]=vos[i].getDomainId();
        	values[i][1]=vos[i].getSequenceName();
        	values[i][2]=vos[i].getUpdateDate();
        	values[i][3]=vos[i].getCycleType();
        	values[i][4]=vos[i].getCurrentValue();
        	values[i][5]=vos[i].getCurrentFmtValue();
        	values[i][6]=vos[i].getSequenceRule();
        	values[i][7]=vos[i].getSequenceLen();
        	values[i][8]=vos[i].getRemark();
        }
		RmProjectHelper.getCommonServiceInstance().doUpdateBatch(SQL_INSERT, values);
        return null;
    }
    
    /**
     * 删除单条记录
     * 
     * @param id 用于删除的记录的id
     * @return 成功删除的记录数
     */
    public int delete(String domainId,String sequenceName) {
    	Object[] values={domainId,sequenceName};
    	return RmProjectHelper.getCommonServiceInstance().doUpdate(SQL_DELETE, values);
    }

    /**
     * 删除多条记录
     * 
     * @param id 用于删除的记录的id
     * @return 成功删除的记录数
     */
    public int delete(String domainIds[],String sequenceNames[]) {
    	if(domainIds == null || domainIds.length == 0) {
            return 0;
        }
        Object[][] values;
        values= new Object[domainIds.length][];
        for(int i=0;i<values.length;i++){
        	values[i] = new Object[2];
        	values[i][0]=domainIds[i];
        	values[i][1]=sequenceNames[i];
        }
		int[] delRows = RmProjectHelper.getCommonServiceInstance().doUpdateBatch(SQL_DELETE, values);
		int sum=0;
		for(int num:delRows){
			sum +=num;
		}
		return sum;
    }

    /**
     * 更新单条记录
     * 
     * @param vo 用于更新的VO对象
     * @return 成功更新的记录数
     */
    public int update(ImpulseSenderVo vo) {
    	Object values[]={vo.getDomainId(),vo.getSequenceName(),vo.getCycleType(),vo.getCurrentValue(),vo.getSequenceRule(),vo.getSequenceLen(),vo.getRemark(),vo.getDomainId(),vo.getSequenceName()};
        return RmProjectHelper.getCommonServiceInstance().doUpdate(SQL_UPDATE, values);
    }

    /**
     * 批量更新修改多条记录
     * 
     * @param vos 添加的VO对象数组
     * @return 成功更新的记录数组
     */
    public int update(ImpulseSenderVo[] vos) {
    	Object[][] values;
        values= new Object[vos.length][];
        for(int i=0;i<values.length;i++){
        	values[i] = new Object[9];
        	values[i][0]=vos[i].getDomainId();
        	values[i][1]=vos[i].getSequenceName();
        	values[i][2]=vos[i].getCycleType();
        	values[i][3]=vos[i].getCurrentValue();
        	values[i][4]=vos[i].getSequenceRule();
        	values[i][5]=vos[i].getSequenceLen();
        	values[i][6]=vos[i].getRemark();
        	values[i][7]=vos[i].getDomainId();
        	values[i][8]=vos[i].getSequenceName();
        }
		int[] updateRows = RmProjectHelper.getCommonServiceInstance().doUpdateBatch(SQL_UPDATE, values);
		int sum=0;
		for(int num:updateRows){
			sum +=num;
		}
		return sum;
    }

    /**
     * 根据Id进行查询
     * 
     * @param id 用于查找的id
     * @return 查询到的VO对象
     */
    public ImpulseSenderVo get(String domainId,String sequenceName) {
    	String sql=SQL_GETBYID;
    	sql = sql.replaceFirst("[?]", "'"+domainId+"'");
    	sql = sql.replaceFirst("[?]", "'"+sequenceName+"'");
    	
    	RmCommonVo commVo = RmProjectHelper.getCommonServiceInstance().doQueryForObject(sql);
    	if(commVo!=null){
    		ImpulseSenderVo vo = new ImpulseSenderVo();
    		vo.setCurrentFmtValue(commVo.getString(FIELD_CURRENT_FMT_VALUE));
    		vo.setCurrentValue((Integer)commVo.get(FIELD_CURRENT_VALUE));
    		vo.setCycleType(commVo.getString(FIELD_CYCLE_TYPE));
    		vo.setDomainId(commVo.getString(FIELD_DOMAIN_ID));
    		vo.setRemark(commVo.getString(FIELD_REMARK));
    		vo.setSequenceLen((Integer)commVo.get(FIELD_SEQUENCE_LEN));
    		vo.setSequenceName(commVo.getString(FIELD_SEQUENCE_NAME));
    		vo.setSequenceRule((String)commVo.getString(FIELD_SEQUENCE_RULE));
    		vo.setUpdateDate((Date)commVo.get(FIELD_UPDATE_DATE));
    		vo.setDomainName(commVo.getString(FIELD_DOMAIN_NAME));
    		return vo;
    	}
        return null;
    }
    
    /**
     * 查询总记录数，带查询条件
     * 
     * @param searchPara 查询条件Map
     * @return 总记录数
     */
    public int getCount(Map<String, Object> searchPara) {
    	String sql="select count(*) from "+ TABLE_NAME +" where 1= 1";
    	sql = makeWhere(searchPara, sql);
        return RmProjectHelper.getCommonServiceInstance().doQueryForInt(sql);
    }
    /**
     * 构造where子句
     * @param searchPara
     * @param sql
     * @return
     */
	private String makeWhere(Map<String, Object> searchPara, String sql) {
		if(!StringHelper.isEmpty((String)searchPara.get("currentFmtValue"))){
    		sql=sql+" and "+FIELD_CURRENT_FMT_VALUE+"='"+searchPara.get("currentFmtValue")+"'";
    	}
    	if(!StringHelper.isEmpty((String)searchPara.get("currentValue"))){
    		sql=sql+" and "+FIELD_CURRENT_VALUE+"='"+searchPara.get("currentValue")+"'";
    	}
    	if(!StringHelper.isEmpty((String)searchPara.get("cycleType"))){
    		sql=sql+" and "+FIELD_CYCLE_TYPE+"='"+searchPara.get("cycleType")+"'";
    	}
		if(!StringHelper.isEmpty((String)searchPara.get("domainId"))){
    		sql=sql+" and "+FIELD_DOMAIN_ID+"='"+searchPara.get("domainId")+"'";
    	}
    	if(!StringHelper.isEmpty((String)searchPara.get("sequenceLen"))){
    		sql=sql+" and "+FIELD_SEQUENCE_LEN+"="+searchPara.get("sequenceLen");
    	}
    	if(!StringHelper.isEmpty((String)searchPara.get("sequenceName"))){
    		sql=sql+" and "+FIELD_SEQUENCE_NAME+" like '%"+searchPara.get("sequenceName")+"%'";
    	}

		if(!StringHelper.isEmpty((String)searchPara.get("sequenceRule"))){
    		sql=sql+" and "+FIELD_SEQUENCE_RULE+" like '%"+searchPara.get("sequenceRule")+"%'";
    	}
    	if(!StringHelper.isEmpty((String)searchPara.get("updateDate"))){
    		sql=sql+" and "+FIELD_UPDATE_DATE+"='"+searchPara.get("updateDate")+"'";
    	}
    	if(!StringHelper.isEmpty((String)searchPara.get("remark"))){
    		sql=sql+" and "+FIELD_REMARK+" like '%"+searchPara.get("remark")+"%'";
    	}

		return sql;
	}    
    /**
     * 功能: 通过组合后的查询条件获得所有的VO对象列表，带翻页，带排序字符
     *
     * @param searchPara 查询条件Map
     * @param orderStr 排序字符
     * @param startIndex 开始位置(第一条是1，第二条是2...)
     * @param size 查询多少条记录(size小于等于0时,忽略翻页查询全部)
     * @param selectAllClumn 是否查询所有列，即 SELECT * FROM ...(适用于导出)
     * @return 查询到的VO列表
     */
    public List<ImpulseSenderVo> list(Map<String, Object> searchPara, String orderStr, int startIndex, int size, boolean allClumn) {
    	searchPara.put("orderStr", orderStr);
        String sql="select sender.*,domain.name as domain_name from "+TABLE_NAME+" sender,"+IDomainConstants.TABLE_NAME+" domain where sender."+FIELD_DOMAIN_ID+"=domain."+IDomainConstants.FIELD_ID;
        sql = makeWhere(searchPara, sql);
        if(!StringHelper.isEmpty(orderStr)){
        	sql =sql+" order by "+orderStr;
        }
        List<RmCommonVo> vos = RmProjectHelper.getCommonServiceInstance().doQueryStartIndex(sql, startIndex, size);
        List<ImpulseSenderVo> retVos = new ArrayList<ImpulseSenderVo>();;
        if(vos!=null){
        	for(RmCommonVo vo:vos){
        		ImpulseSenderVo impulseSenderVo = new ImpulseSenderVo();
        		impulseSenderVo.setCurrentFmtValue(vo.getString(FIELD_CURRENT_FMT_VALUE));
        		impulseSenderVo.setCurrentValue((Integer)vo.get(FIELD_CURRENT_VALUE));
        		impulseSenderVo.setCycleType(vo.getString(FIELD_CYCLE_TYPE));
        		impulseSenderVo.setDomainId(vo.getString(FIELD_DOMAIN_ID));
        		impulseSenderVo.setRemark(vo.getString(FIELD_REMARK));
        		impulseSenderVo.setSequenceLen((Integer)vo.get(FIELD_SEQUENCE_LEN));
        		impulseSenderVo.setSequenceName(vo.getString(FIELD_SEQUENCE_NAME));
        		impulseSenderVo.setSequenceRule((String)vo.getString(FIELD_SEQUENCE_RULE));
        		impulseSenderVo.setUpdateDate((Date)vo.get(FIELD_UPDATE_DATE));
        		impulseSenderVo.setDomainName(vo.getString(FIELD_DOMAIN_NAME));
        		retVos.add(impulseSenderVo);
        	}
        }
        return retVos;
    }
    
}
